package com.hxd.otc.module.my.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.PhotoActivity;
import com.hxd.otc.module.my.bean.UploadImageBean;
import com.hxd.otc.module.my.contract.SilverCertificationPhotoPickerContract;
import com.hxd.otc.module.my.presenter.SilverCertificationPhotoPickerPresenter;
import com.hxd.otc.utils.GlideUtils;
import com.hxd.otc.utils.LubanUtils;
import com.hxd.otc.widget.TitleBar;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class SilverCertificationPhotoPickerActivity extends PhotoActivity<SilverCertificationPhotoPickerPresenter> implements SilverCertificationPhotoPickerContract.IView {

    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.iv_face)
    ImageView mIvFace;
    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.iv_hold)
    ImageView mIvHold;
    @BindView(R.id.tv_next)
    TextView mTvNext;
    @BindView(R.id.scrollView_face_example)
    View mScrollViewFaceExample;
    @BindView(R.id.cl_hold_example)
    View mClHoldExample;

    public static final String IMAGE_TAG_FACE = "face";
    public static final String IMAGE_TAG_BACK = "back";
    public static final String IMAGE_TAG_HOLD = "hold";

    private String mImageTag;

    public static final String RESULT_EXTRA_PHOTO_MAPS = "extra_photo_maps";

    private HashMap<String, UploadImageBean> mImageMaps = new HashMap<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sliver_certification_photo_picker;
    }

    @Override
    protected SilverCertificationPhotoPickerPresenter createPresenter() {
        return new SilverCertificationPhotoPickerPresenter(this);
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
        mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {
            }

            @Override
            public void onRight(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.iv_face, R.id.iv_back, R.id.iv_hold, R.id.tv_next, R.id.tv_tip_1, R.id.tv_tip_2, R.id.cl_face_example, R.id.cl_hold_example})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_face:
                mImageTag = IMAGE_TAG_FACE;
                showPhotoDialog();
                break;
            case R.id.iv_back:
                mImageTag = IMAGE_TAG_BACK;
                showPhotoDialog();
                break;
            case R.id.iv_hold:
                mImageTag = IMAGE_TAG_HOLD;
                showPhotoDialog();
                break;
            case R.id.tv_tip_1:
                mScrollViewFaceExample.setVisibility(View.VISIBLE);
                break;
            case R.id.cl_face_example:
                mScrollViewFaceExample.setVisibility(View.GONE);
                break;
            case R.id.tv_tip_2:
                mClHoldExample.setVisibility(View.VISIBLE);
                break;
            case R.id.cl_hold_example:
                mClHoldExample.setVisibility(View.GONE);
                break;
            case R.id.tv_next:
                onNext();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (mScrollViewFaceExample.getVisibility() == View.VISIBLE) {
            mScrollViewFaceExample.setVisibility(View.GONE);
        } else if (mClHoldExample.getVisibility() == View.VISIBLE) {
            mClHoldExample.setVisibility(View.GONE);
        } else {
            super.onBackPressed();
        }
    }

    private void onNext() {
        if (!mImageMaps.containsKey(IMAGE_TAG_FACE)) {
            showToast(getString(R.string.please_select_real_name_face));
            return;
        }

        if (!mImageMaps.containsKey(IMAGE_TAG_BACK)) {
            showToast(getString(R.string.please_select_real_name_back));
            return;
        }

        if (!mImageMaps.containsKey(IMAGE_TAG_HOLD)) {
            showToast(getString(R.string.please_select_real_name_hold));
            return;
        }

        if (checkAllUploadSuccess()) {
            skipNextPage();
        } else {
            for (Map.Entry<String, UploadImageBean> imageBeanEntry : mImageMaps.entrySet()) {
                final UploadImageBean value = imageBeanEntry.getValue();

                if (TextUtils.isEmpty(value.getRemotePath())) {
                    showSpinnerDlg(getString(R.string.be_uploading), false);
                    LubanUtils lubanUtils = new LubanUtils();
                    ArrayList<String> list = new ArrayList<>();
                    list.add(value.getLocalFile().getAbsolutePath());
                    lubanUtils.withRx(SilverCertificationPhotoPickerActivity.this, list, new Consumer<List<File>>() {
                        @Override
                        public void accept(List<File> files) throws Exception {
                            mPresenter.uploadImage(value.getTag(), files.get(0));
                        }
                    });
                }
            }
        }
    }

    private void skipNextPage() {
        Intent intent = new Intent();
        intent.putExtra(RESULT_EXTRA_PHOTO_MAPS, mImageMaps);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    protected void openDialogCamera() {
        openCamera();
    }

    @Override
    protected void openDialogAlbum() {
        openAlbum(1);
    }

    @Override
    protected void imageFileResult(@NonNull List<File> imageFiles) {
        ImageView imageView = getUpdateImageView();
        UploadImageBean uploadImageBean = new UploadImageBean(imageFiles.get(0), mImageTag);
        mImageMaps.put(mImageTag, uploadImageBean);
        GlideUtils.loadNormal(this, uploadImageBean.getLocalFile().getAbsolutePath(), imageView);
    }

    @Nullable
    private ImageView getUpdateImageView() {
        ImageView imageView = null;
        switch (mImageTag) {
            case IMAGE_TAG_FACE:
                imageView = mIvFace;
                break;
            case IMAGE_TAG_BACK:
                imageView = mIvBack;
                break;
            case IMAGE_TAG_HOLD:
                imageView = mIvHold;
                break;
        }
        return imageView;
    }

    @Override
    public void uploadImageFail(String tag, String message) {
        dismissSpinnerDlg();
        switch (tag) {
            case IMAGE_TAG_FACE:
                showToast(getString(R.string.upload_real_name_face_fail));
                break;
            case IMAGE_TAG_BACK:
                showToast(getString(R.string.upload_real_name_back_fail));
                break;
            case IMAGE_TAG_HOLD:
                showToast(getString(R.string.upload_real_name_hold_fail));
                break;
        }
    }


    @Override
    public void uploadImageSuccess(String tag, String result) {
        switch (tag) {
            case IMAGE_TAG_FACE:
                showToast(getString(R.string.upload_real_name_face_success));
                break;
            case IMAGE_TAG_BACK:
                showToast(getString(R.string.upload_real_name_back_success));
                break;
            case IMAGE_TAG_HOLD:
                showToast(getString(R.string.upload_real_name_hold_success));
                break;
        }
        mImageMaps.get(tag).setRemotePath(result);
        if (checkAllUploadSuccess()) {
            dismissSpinnerDlg();
            skipNextPage();
        }
    }

    private boolean checkAllUploadSuccess() {
        for (Map.Entry<String, UploadImageBean> imageBeanEntry : mImageMaps.entrySet()) {
            final UploadImageBean value = imageBeanEntry.getValue();
            if (TextUtils.isEmpty(value.getRemotePath())) {
                return false;
            }
        }
        return true;
    }
}
